package com.kzx;

import java.util.Calendar;

public class TestMaxDays {

    public static void main(String[] args) {
//        int maxDaysByDate = getDaysByYearMonth(2020, 6);
//        System.out.println(maxDaysByDate);
        int maxDaysByDate = getDaysByYearMonth("2020-02");
        System.out.println(maxDaysByDate);
    }

    //2020-06
    public static int getDaysByYearMonth(String strCreBeginDate) {
        String strYEAR = strCreBeginDate.substring(0,4);
        String strMONTH = strCreBeginDate.substring(5);

        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, Integer.valueOf(strYEAR));
        a.set(Calendar.MONTH, Integer.valueOf(strMONTH) - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }
    /**
     * 根据年 月 获取对应的月份 天数
     * */
    public static int getDaysByYearMonth(int year, int month) {

        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }
}
